from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor
import os
import time


def thread_task(task_id):
    pid = os.getpid()
    print(f"进程 {pid} 的线程正在处理任务 {task_id}")
    time.sleep(1)
    return f"进程 {pid} 的任务 {task_id} 结果"


def process_task():
    pid = os.getpid()
    print(f"启动进程 {pid}")
    results = []

    # 使用线程池
    with ThreadPoolExecutor(max_workers=3) as executor:
        futures = [executor.submit(thread_task, i) for i in range(3)]
        results = [f.result() for f in futures]

    print(f"进程 {pid} 完成")
    return results


if __name__ == "__main__":
    # 使用进程池
    with ProcessPoolExecutor(max_workers=2) as executor:
        futures = [executor.submit(process_task) for _ in range(2)]

        for future in futures:
            print(future.result())
